package com.test.cordova.plugins;

import android.content.Intent;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 启动目标activity
 */
public class StartActivityPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("start")) {

            this.start(args, callbackContext);
            return true;
        }
        return false;
    }

    private void start(JSONArray message, CallbackContext callbackContext) {
        try {
            if (message != null && message.length() > 0) {
                JSONObject root = message.getJSONObject(0);
                String action = root.getString("action");
                Intent it = new Intent();
                it.setAction(action);
                it.addCategory("android.intent.category.DEFAULT");
                cordova.getActivity().startActivity(it);
                //callbackContext.success(message);
            } else {
                callbackContext.error("Expected one non-empty string argument.");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
